package com.example.springbootwebrestfulcrud.config;

import com.example.springbootwebrestfulcrud.component.LoginHandlerInterceptor;
import com.example.springbootwebrestfulcrud.component.MyLocaleResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration;
import org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer;
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @Auther: luzhanshi
 * @Date: 2021/1/31 - 01 - 31 - 19:34 
 * @Description: com.example.springbootwebrestfulcrud.config
 * @version: 1.0
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //public class MyMvcConfig extends WebMvcConfigurerAdapter {
//    注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        super.addInterceptors(registry);
        /**
         * 关于静态资源：**.css **.js。。。
         * springboot已经自动配置了静态资源的映射，不会被拦截，所以下面我们忽略项里面不要添加静态资源路径
         */
        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/", "/error", "/index.html", "/user/login", "/hello", "/hello2", "/sucess", "/favicon.ico"

                );
    }

    @Bean
    public WebMvcConfigurer webMvcConfigurerAdapter() {

        return new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
                registry.addViewController("/main.html").setViewName("dashboard");
            }
        };
    }

    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }
    //一定要将这个定制器加入到容器中
@Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> webServerFactoryCustomizer(){
        return new WebServerFactoryCustomizer<TomcatServletWebServerFactory>() {
            @Override
            public void customize(TomcatServletWebServerFactory factory) {

//                factory.setPort(9999);
            }
        };
}

}
